<?php

namespace App\Admin\Actions\Grid\Tools;

use App\Admin\Forms\ExportAgreementForm;
use App\Admin\Forms\ToPayBatchToolsForm;
use App\Admin\Forms\UploadReceiptBatchToolsForm;
use Dcat\Admin\Grid\BatchAction;
use Dcat\Admin\Support\Helper;
use Dcat\Admin\Widgets\Modal;
use Dcat\Admin\Grid\RowAction;
use Illuminate\Support\Arr;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Route;
use function Symfony\Component\Routing\Loader\Configurator\collection;

class UploadReceiptFormBatchTools extends RowAction
{

    protected $button = '<div class="btn-group filter-button-group dropdown" style="margin-right:3px">
    <button class="btn btn-primary filter-btn-xIWquMFr btn-outline">
        <i class="feather glyphicon-export"></i><span class="d-none d-sm-inline">&nbsp;&nbsp;批量上传回执单</span>
        <span class="filter-count"></span>
    </button>
    </div>';


    public function render()
    {
        $form = UploadReceiptBatchToolsForm::make();
        $form->appendHtmlAttribute('class', 'receipt-form');
        return Modal::make()
            ->lg()
            ->title($this->title)
            ->body($form)
            ->onLoad($this->getModalScript())
            ->button($this->button);
    }

    protected function getModalScript()
    {
        // 弹窗显示后往隐藏的id表单中写入批量选中的行ID
        return <<<JS

            // 获取选中的ID数组
        var key = {$this->getSelectedKeysScript()}
        console.log('key', key);
        $('#reset-password-id').val(key);

JS;
    }


    /**
     * {@inheritdoc}
     */
    protected function actionScript()
    {
        $warning = __('No data selected!');

        return <<<JS
function (data, target, action) {
    var key = {$this->getSelectedKeysScript()}

    if (key.length === 0) {
        Dcat.warning('{$warning}');
        return false;
    }

    // 设置主键为复选框选中的行ID数组
    action.options.key = key;
}
JS;
    }

    /**
     * @return string
     */
    public function getSelectedKeysScript()
    {
        return "Dcat.grid.selected('{$this->parent->getName()}')";
    }
}
